using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using DBUtility;
using Model;
using MySql.Data.MySqlClient;

namespace BLL
{
    public class SingleBatchEntryBLL
    {
        private BatchRepository batchRepository = new BatchRepository();
        //private DeviceRepository deviceRepository = new DeviceRepository();
        private ProcessFlowRepository flowRepository = new ProcessFlowRepository();
        private TotalProcessRepository totalProcessRepository = new TotalProcessRepository();
        private StationRepository stationRepository = new StationRepository();
        private ProcessFlowRepository processFlowRepository = new ProcessFlowRepository();
        private EquipmentRepository equipmentRepository = new EquipmentRepository();
        private ProcessPackageRepository processPackageRepository = new ProcessPackageRepository();
        private ProductRepository productRepository = new ProductRepository();
        // 获取批次表所有数据
        public Batch showBatchMeg(int batchId)
        {
            return batchRepository.GetBatchById(batchId);
        }
        //获取所有总工艺流程数据
        public TotalProcess GetTotalProcessById(int totalProcessId)
        {
            return totalProcessRepository.GetTotalProcessById(totalProcessId);
        }
        //根据id查找工站数据
        public Station GetStationById(int stationId)
        {
            return stationRepository.GetStationById(stationId);
        }
        //根据流程iD查找流程数据
        public ProcessFlow GetProcessFlowById(int flowId)
        {
            return processFlowRepository.GetProcessFlowById(flowId);
        }
        //获取所有总工艺流程数据
        public List<Equipment> GetAllEquipments()
        {
            return equipmentRepository.GetAllEquipments();
        }
        // 根据设备类型查找设备ID
        public int GetEquipmentIdByModel(string equipmentType)
        {
            string sql = "SELECT EquipmentId FROM equipment WHERE EquipmentType = @EquipmentType";
            MySqlParameter[] parameters = { new MySqlParameter("@EquipmentType", equipmentType) };
            var result = DBHelper.ExecuteScalar(sql, parameters);
            return result != null ? Convert.ToInt32(result) : -1; // 返回-1表示未找到
        }

        //根据设备iD查找设备数据
        public Equipment GetEquipmentById(int equipmentId)
        {
            return equipmentRepository.GetEquipmentById(equipmentId);
        }
        // 根据程序包名查找程序包ID
        public int GetProcessPackageIdByName(string packageName)
        {
            string sql = "SELECT ProcessPackageId FROM processpackage WHERE Name = @PackageName";
            MySqlParameter[] parameters = { new MySqlParameter("@PackageName", packageName) };
            var result = DBHelper.ExecuteScalar(sql, parameters);
            return result != null ? Convert.ToInt32(result) : -1; // 返回-1表示未找到
        }
        //根据程序包id查找程序包数据
        public ProcessPackage GetProcessPackageById(int processPackageId)
        {
           return processPackageRepository.GetProcessPackageById(processPackageId);
        }
        // 根据用户名查找用户ID
        public int GetUserIdByUsername(string username)
        {
            string sql = "SELECT UserId FROM user WHERE Username = @Username";
            MySqlParameter[] parameters = { new MySqlParameter("@Username", username) };
            var result = DBHelper.ExecuteScalar(sql, parameters);
            return result != null ? Convert.ToInt32(result) : -1; // 返回-1表示未找到
        }
        // 根据 ProductId 获取产品信息
        public Product GetProductById(int productId)
        {
            return productRepository.GetProductById(productId);
        }
        /// 查询所有总流程
        public List<Station> GetStationByTotolId(int totolId)
        {
            return stationRepository.GetStationByTotolId(totolId);
        }
        // 更新产品的进出站状态
        public int UpdateProductEntryAndExitStatus(int productId, string entryAndExitStatus)
        {
            string sql = @"UPDATE product 
                            SET EntryAndExitStatus = @EntryAndExitStatus 
                          WHERE ProductId = @ProductId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@EntryAndExitStatus", entryAndExitStatus),
                new MySqlParameter("@ProductId", productId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }
         // 根据总流程 ID 和步骤序号获取下一个工站的 ID
        public int GetNextStationIdByTotalProcessIdAndStepNumber(int totalProcessId, int currentStepNumber)
        {
            // 获取下一个步骤序号
            int nextStepNumber = currentStepNumber + 1;

            // 获取下一个工站的 ID
            return stationRepository.GetStationIdByTotalProcessIdAndStepNumber(totalProcessId, nextStepNumber);
        }
        // 根据产品ID获取当前工站ID
        public int GetStationIdByProductId(int productId)
        {
            Debug.WriteLine("这个是产品id：", productId);
            // 获取产品信息
            Product product = productRepository.GetProductById(productId);
            Debug.WriteLine("这是测试产品数据：", product.ProductId);
            if (product == null)
            {
                return -1; // 未找到产品
            }

            // 获取当前工站ID
            return product.StationId;
        }
        //根据工站Id获取工艺流程Id
        public int GetFlowIdByStationId(int stationId)
        {
            return processFlowRepository.GetFlowIdByStationId(stationId);
        }
        //更新产品数据
        public int UpdateProduct(Product product)
        {
            return productRepository.UpdateProduct(product);
        }
        // 获取所有总工艺流程数据
        public List<TotalProcess> GetAllTotalProcesses()
        {
            return totalProcessRepository.GetAllTotalProcesses();
        }

        // 查询所有工艺流程
        public List<ProcessFlow> GetAllProcessFlows()
        {
            string sql = "SELECT * FROM processflow";
            return processFlowRepository.GetAllProcessFlows();
        }
    }
}
